package com.zr.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 杜雨萌
 * @date 2022/10/29
 * @version 2022.1
 */
public class EpidemicSituationUtil {

    /**
     * 得到传入的城市疫情信息
     *
     * @param city
     * @return
     * @throws IOException
     */
    public static Map<String, Object> getTransCityData(String city) throws IOException {
        JSONArray localCityNCOVDataList = getAllCityData();
        //城市map添加今日新增数量信息和是否是高风险城市
        Map<String, Object> transCityMap = new HashMap<>();
        for (int i = 0; i < localCityNCOVDataList.size(); i++) {
            JSONObject objectInArray = localCityNCOVDataList.getJSONObject(i);
            //判断城市是否是查询城市或者中转站
            if (objectInArray.getString("city").equals(city)) {
                //添加新增数量信息
                transCityMap.put("local_confirm_add", objectInArray.getString("local_confirm_add"));
                //添加高危城市，返回false或true
                transCityMap.put("isSpecialCity", objectInArray.getString("isSpecialCity"));
                transCityMap.put("city", city);
            }
        }
        return transCityMap;
    }

    /**
     * 得到疫情城市列表
     * @param city
     * @return
     * @throws IOException
     * @author 杜雨萌
     */
    public static Map<String, Object> getSpecialCityList(String city) throws IOException {
        JSONArray localCityNCOVDataList = getAllCityData();
        //是什么map
        Map<String, Object> specialCityMap = new HashMap<>();
        for (int i = 0; i < localCityNCOVDataList.size(); i++) {
            JSONObject objectInArray = localCityNCOVDataList.getJSONObject(i);
            //判断城市是否是高风险城市
            if (objectInArray.getString("city").equals(city) && objectInArray.getString("isSpecialCity").equals("true")) {
                //添加新增数量信息
                specialCityMap.put("local_confirm_add", objectInArray.getString("local_confirm_add"));
                //添加高危城市，返回false或true
                specialCityMap.put("isSpecialCity", objectInArray.getString("isSpecialCity"));
                //添加当前城市
                specialCityMap.put("city", city);
            }
        }
        return specialCityMap;
    }

    /**
     * 和获得所有城市信息
     * @return
     * @throws IOException
     * @author 杜雨萌
     */
    public static JSONArray getAllCityData() throws IOException {
        //1 发送请求 连接 获得疫情数据
        String resultBody = Jsoup.connect("https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=localCityNCOVDataList,diseaseh5Shelf")
                .ignoreContentType(true)
                .execute().body();
        //2 转换成Object类型
        JSONObject jsonObject = JSON.parseObject(resultBody);
        //3 获取data部分
        JSONObject data = jsonObject.getJSONObject("data");
        //4 获取高风险地区数据
        JSONArray localCityNCOVDataList = data.getJSONArray("localCityNCOVDataList");
        return localCityNCOVDataList;
    }
}